const apiStatus = require('../../apiStatus');
const validHelper = require('../../helpers/validHelper');
const productService = require('./productService');
const featureService = require('./featureService');
const categoryService = require('./categoryService');

function setup(app, appRouter){
    appRouter.post('/api/product/list', async(ctx,next) => {
        var appId = 2;//ctx.appInfo.id;
        var categoryId = ctx.request.body.categorys || '';
        var brandId = ctx.request.body.brands || '';
        var keyName = ctx.request.body.keyName || '';

        var list = await productService.list(appId,categoryId,brandId,keyName);
        ctx.apiBody = list;
        next();
    });

    appRouter.post('/api/product/info', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let productId = ctx.request.body.productId;
        let userId = 1;
        let miss = [];
        miss = validHelper.check([
          productId,"productId"
        ]);

        if (miss.length > 0 ) {
          ctx.body = {apiStatus: apiStatus.argumentInvalid};
          return;
        }

        var info = await productService.info(appId,productId,userId);
        if (info == null) {
          ctx.body = {apiStatus: apiStatus.serverError};
          return;
        } else {
          ctx.apiBody = info;
        }
        next();
    });

    appRouter.post('/api/feature/list',async(ctx,next) => {
      var list = await featureService.list();
      if (list == null) {
        ctx.body = {apiStatus: apiStatus.serverError};
        return;
      } else {
        ctx.apiBody = list;
      }
      next();
    });

    appRouter.post('/api/category/list',async(ctx,next) => {
      let appId = 2;//ctx.appInfo.id;
      let list = await categoryService.list(appId);
      if (list == null) {
        ctx.apiBody = [];
        return;
      }
      ctx.apiBody = list;
      next();
    });

    appRouter.post('/api/favorite/list', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let userId = 1;//ctx.user.id;
        let pageIndex = ctx.request.body.pageIndex || 0;
        let pageSize = ctx.request.body.pageSize || 10;

        pageSize = Math.min(100,Math.max(1,pageSize));
  	    let offset = pageIndex * pageSize;
  	    let limit = pageSize;
        let rows = await productService.getFavorite(appId,userId,offset,limit);
        ctx.apiBody = rows;
        next();
    });

    appRouter.post('/api/favorite/add', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let userId = 1;//ctx.user.id;
        let productId = ctx.request.body.productId;
        let skuId = ctx.request.body.sId;
        let addTime = Math.floor(new Date().getTime() / 1000);

        let flag = await productService.addFavorite(appId,userId,productId,skuId);
        ctx.apiBody = {apiStatus: flag ? apiStatus.success : apiStatus.serverError};
        next();
    });

    appRouter.post('/api/favorite/del', async(ctx,next) => {
        let appId = 2;//ctx.appInfo.id;
        let userId = 1;//ctx.user.id;
        let productId = ctx.request.body.productId;
        let skuId = ctx.request.body.sId;
        let addTime = Math.floor(new Date().getTime() / 1000);

        let flag = await productService.delFavorite(appId,userId,productId,skuId);
        ctx.apiBody = {apiStatus: flag ? apiStatus.success : apiStatus.serverError};
        next();
    });
}

module.exports = setup;
